//
//  mttPoint.m
//  mtt
//
//  Created by Gerfried Mikusch on 29.09.10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "mttPoint.h"



@implementation mttPoint

@synthesize pinID, pinLoc, tapRecognizer, delegate;



#pragma mark -
#pragma mark gesture recognition

// Ein TAP löscht den pin
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
	
	//Wenn Recognized und eingeblendet
	if (recognizer.state == UIGestureRecognizerStateRecognized && self.layer.opacity > 0.0) {
	
		int pin;
		pin=pinID;
		NSLog(@"Hier soll der Pin dann geöscht werden!!!!! %i",pin);
		[delegate removePin:pinID];
		
		
	}
}


#pragma mark -
#pragma mark init & dealloc

-(id)initWithFrame:(CGRect)frame {
	
	if ((self = [super initWithFrame:frame])) {
		
		[self setUserInteractionEnabled:YES];
		[self setMultipleTouchEnabled:YES];
		
		
		//recognizer für taps mit einem Finger
		UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
		[self addGestureRecognizer:recognizer];
		self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
		self.tapRecognizer.numberOfTouchesRequired = 1;
		recognizer.delegate = self;
		[recognizer release];
		
		
		[self setAutoresizingMask:UIViewAutoresizingNone];
		
		self.opaque = NO;
		
    }
    return self;
	
	
}


- (void) dealloc {
	
	[tapRecognizer release];
	[super dealloc];
	
}


#pragma mark -
#pragma mark hittest

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

	return self;
	
}

- (void) initWithID:(int)pinNr {
	
}
-(void) setLocationOfPin:(CGPoint)loc {
	
}

#pragma mark -
#pragma mark drawings

- (void)drawRect:(CGRect)rect {
	
	CGContextRef context = UIGraphicsGetCurrentContext();		//Context for drawing
	CGContextSetShadow(context, CGSizeMake(2, 2), 3);			//Set the shadow for the context
	
	CGRect circleRect = CGRectMake(2, 2, 30, 30);
	
	//Kreisumriss
	CGContextSetLineWidth(context, 3);
	CGFloat componentsStroke[4] = {0, 0, 0, 100};
	CGContextSetStrokeColor(context, componentsStroke);
	CGContextStrokeEllipseInRect(context, circleRect);
	
	//Layer für die Kreisfüllung
	CGLayerRef circleLayer = CGLayerCreateWithContext(context, circleRect.size, nil);		//layer for the circle
	CGContextRef circleContext = CGLayerGetContext(circleLayer);					//Context for the circle
	
	//Pfad für den Kreis
	CGMutablePathRef circlePath = CGPathCreateMutable();
	CGPathAddEllipseInRect(circlePath, NULL, CGRectMake(0, 0, 30, 30));
	CGContextAddPath(circleContext, circlePath);					//Add Path to context of the path Layer
	
	CGContextClip(circleContext);								//Clip the Path-Context to the Path. Necessary to fill it with a Gradient.
	
	//Gradient
	CGFloat componentsFill[10] = { 
		0,		//CYAN-Value of brighter Color
		0,		//MAGENTA-Value of brighter Color
		1,		//YELLOW-Value of brighter Color
		0.28,	//BLACK-Value of brighter Color
		1,		//ALPHA-Value of brighter Color
		0,		//CYAN-Value of darker Color
		0,		//MAGENTA-Value of darker Color
		1,		//YELLOW-Value of darker Color
		0.66,	//BLACK-Value of darker Color
		1		//ALPHA-Value of darker Color
	};
	
	CGFloat locations[] = { 0.2, 1 };	//Locations of Gradient Color 
	
	CGGradientRef gradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceCMYK(), componentsFill, locations, 2);
	
	//Verlaufsfüllung für den Kreis
	CGContextDrawLinearGradient(circleContext, gradient, CGPointMake(30, 30), CGPointMake(0, 0), kCGGradientDrawsAfterEndLocation);
	
	//Layer für die Linie
	CGLayerRef lineLayer = CGLayerCreateWithContext(context, self.frame.size, nil);		//layer for the circle
	CGContextRef lineContext = CGLayerGetContext(lineLayer);
	
	//Linie
	CGContextSetLineWidth(lineContext, 4);
	
	CGContextBeginPath(lineContext);
	CGContextMoveToPoint(lineContext, 5, 17);
	CGContextAddLineToPoint(lineContext, 30, 17);
	CGContextStrokePath(lineContext);
	
	 
	//Zeichne die Linie
	CGContextDrawLayerInRect(circleContext, circleRect, lineLayer);

	
	//Zeichne den Kreis
	CGContextDrawLayerInRect(context, circleRect, circleLayer);
	
}







@end
